GitHub | 您所在的位置:网站首页 › socket io client java › GitHub |
Engine.IO-client Java
This is the Engine.IO Client Library for Java, which is simply ported from the JavaScript client. See also: Socket.IO-client Java Table of content Compatibility Installation Maven Gradle Usage Features License Compatibility Client version Engine.IO server Socket.IO server 0.9.x 1.x 1.x 1.x 3.x 2.x 2.x 4.x 3.x InstallationThe latest artifact is available on Maven Central. MavenAdd the following dependency to your pom.xml. io.socket engine.io-client 2.1.0 GradleAdd it as a gradle dependency for Android Studio, in build.gradle: compile ('io.socket:engine.io-client:2.1.0') { // excluding org.json which is provided by Android exclude group: 'org.json', module: 'json' } UsageEngine.IO-client Java has the similar api with the JS client. You can use Socket to connect: socket = new Socket("ws://localhost"); socket.on(Socket.EVENT_OPEN, new Emitter.Listener() { @Override public void call(Object... args) { socket.send("hi"); socket.close(); } }); socket.open();You can listen events as follows: socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() { @Override public void call(Object... args) { String data = (String)args[0]; } }).on(Socket.EVENT_ERROR, new Emitter.Listener() { @Override public void call(Object... args) { Exception err = (Exception)args[0]; } });How to set options: opts = new Socket.Options(); opts.transports = new String[] {WebSocket.NAME}; socket = new Socket(opts);Sending and receiving binary data: socket = new Socket(); socket.on(Socket.EVENT_OPEN, new Emitter.Listener() { @Override public void call(Object... args) { // send binary data byte[] data = new byte[42]; socket.send(data); } }).on(Socket.EVENT_MESSAGE, new Emitter.Listener() { @Override public void call(Object... args) { // receive binary data byte[] data = (byte[])args[0]; } });Use custom SSL settings: OkHttpClient okHttpClient = new OkHttpClient.Builder() .hostnameVerifier(myHostnameVerifier) .sslSocketFactory(mySSLContext.getSocketFactory(), myX509TrustManager) .build(); // default SSLContext for all sockets Socket.setDefaultOkHttpWebSocketFactory(okHttpClient); Socket.setDefaultOkHttpCallFactory(okHttpClient); // set as an option opts = new Socket.Options(); opts.callFactory = okHttpClient; opts.webSocketFactory = okHttpClient; socket = new Socket(opts); FeaturesThis library supports all of the features the JS client does, including events, options and upgrading transport. Android is fully supported. LicenseMIT |
CopyRight 2018-2019 实验室设备网 版权所有 |